package com.action.admin.roles;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.action.BaseAction;
import com.dao.FunctionDao;
import com.dao.RolesDao;
import com.entity.Employee;
import com.entity.Function;
import com.entity.GoodsType;
import com.entity.Role;
import com.util.Page;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;

/**
 * 
 * @author Administrator
 * 
 */
// @ParentPackage("json-default")
@ParentPackage("extendsjson-default")
@Namespace("/roles")
@ResultPath("/WEB-INF/pages")
public class RolesAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(RolesAction.class);

	/**
	 * 增加角色
	 * 
	 * @return
	 */
	@Action(value = "addrole", results = { @Result(name = "success", location = "addrole.jsp") })
	public String addrole() {
		try {
			list = functionDao.findTreeTopNode();// 一级菜单
			map = new HashMap();
			map2 = new HashMap();
			// 二级菜单
			for (Function function : list) {
				list2 = functionDao.findTreeNodeById(function.getId());
				System.out.println("gtype.getId()== " + function.getId());
				map.put(function.getId(), list2);
				// 三级菜单
				for (Function function2 : list2) {
					list3 = functionDao.findTreeNodeById(function2.getId());
					System.out.println("g2type.getId()== " + function2.getId());
					map2.put(function2.getId(), list3);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}

	/**
	 * 删除角色
	 * 
	 * @return
	 */
	@Action(value = "delrole", results = { @Result(name = "success", location = "roleslist.do", type = "redirect") })
	public String delrole() {
		return SUCCESS;
	}

	/**
	 * 保存角色信息
	 * 
	 * @return
	 */
	@Action(value = "saverole", results = { @Result(name = "success", location = "roleslist.do", type = "redirect") })
	public String saverole() {
		try {
			flag = rolesDao.saveRole(role);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 角色列表
	 * 
	 * @return
	 */
	@Action(value = "rolespage", results = { @Result(name = "success", location = "roleslist2.jsp") })
	public String rolespage() {
		Employee emp = (Employee) session("userinfo");
		try {
			page = rolesDao.findRolePage(page);
			if (page != null) {
				page.setPreUrl("rolespage.do?page.currentPage=");
				page.setAppendUrl("");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 角色列表
	 * 
	 * @return
	 */
	@Action(value = "roleslist", results = { 
			@Result(name = "input", location = "roleslist2.jsp"),
			@Result(name = "success", location = "roleslist2.jsp") })
	public String roleslist() {
		try {
			list = rolesDao.findRolelist();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 更新角色信息
	 * 
	 * @return
	 */
	@Action(value = "updaterole", results = { @Result(name = "success", location = "roleslist.do", type = "redirect") })
	public String updaterole() {
		try {
			flag = rolesDao.saveRole(role);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	private RolesDao rolesDao = (RolesDao) ioc.newInstance(RolesDao.class);
	private FunctionDao functionDao = (FunctionDao) ioc.newInstance(FunctionDao.class);
	private boolean flag = false;
	private Role role;
	private Page page = new Page();
	private List<Function> list,list2,list3;
	private Map map,map2;

	public List getList3() {
		return list3;
	}

	public void setList3(List list3) {
		this.list3 = list3;
	}

	public List getList2() {
		return list2;
	}

	public void setList2(List list2) {
		this.list2 = list2;
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public Map getMap2() {
		return map2;
	}

	public void setMap2(Map map2) {
		this.map2 = map2;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}
}
